const { CourseProgress } = require('@/models/course');
const { clearObject } = require('@/utils');

const getProgress = async (req, res) => {
  try {
    const { user_id, user_name, course_id, title, pageNum = 1, pageSize = 20 } = req.query;
    const offset = (pageNum - 1) * pageSize;
    const limit = pageSize;
    const {rows, total} = await CourseProgress.getProgress(user_id, user_name, course_id, title, offset, limit);
    res.status(200).send({rows, total});
  } catch (error) {
    res.status(500).json({ message: '获取课程进度失败', error });
  }
}
// 更新课程进度  用户端
const updateProgress = async (req, res) => {
  try {
    const { id } = req.params;
    const body = req.body;
    const params = clearObject(body);
    const updatedProgress = await CourseProgress.updateProgress(id, params);
    res.status(200).send(updatedProgress);
  } catch (error) {
    res.status(500).json({ message: '更新课程进度失败', error });
  }
};

// 暂未开发
const getProgressAnalytics = async (_, res) => {
  try {
    const progressAnalytics = await CourseProgress.getProgressAnalytics();
    res.status(200).send(progressAnalytics);
  } catch (error) {
    res.status(500).json({ message: '获取课程进度分析失败', error });
  }
}

module.exports = {
  getProgress,
  updateProgress,
  getProgressAnalytics
}